<?php
/**
 * Created by PhpStorm.
 * User: this
 * Date: 2018/12/5
 * Time: 9:49
 */
namespace app\admin\model;

use think\Model;

class Articles extends Model{
    /**
     * @param $condition
     * @param string $filed
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne($condition,$filed = '*'){
        return $this->name('article')->field($filed)->where($condition)->find();
    }
    /**
     * @param $data
     * @return int|string
     */
    public function add($data){
        return $this->name('article')->insert($data);
    }

    /**
     * @param $condition
     * @param string $field
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList($condition,$field = '*'){
        return $this->name('article')->field($field)->cache(true)->where($condition)->select();
    }

    /**
     * @param $condition
     * @param $data
     * @return $this
     */
    public function edit($condition,$data){
        return $this->name('article')->where($condition)->update($data);
    }

    /**
     * @param $condition
     * @param string $field
     * @param int $num
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getPage($condition,$field = '*',$num = 10){
        return $this->name('article')->field($field)->join('bb_article_category','bb_article_category.article_cate_id=bb_article.article_category_id')->where($condition)->order('bb_article.article_id asc')->cache(true,5)->paginate($num);
    }

    /**
     * @param $condition
     * @return int
     */
    public function del($condition){
        return $this->name('article')->where($condition)->delete();
    }

    /**
     * @param $condition
     * @param $field
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cateOne($condition,$field){
        return $this->name('article_category')->field($field)->where($condition)->find();
    }

    /**
     * @param $condition
     * @param string $field
     * @param int $num
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function catePage($condition,$field = '*',$num = 10){
        return $this->name('article_category')->field($field)->where($condition)->order('article_cate_id asc')->cache(true)->paginate($num);
    }

    /**
     * @param $data
     * @return int|string
     */
    public function cateAdd($data){
        return $this->name('article_category')->insert($data);
    }

    /**
     * @param $condition
     * @param string $filed
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cateList($condition,$filed = '*'){
        return $this->name('article_category')->field($filed)->cache(true)->where($condition)->select();
    }

    /**
     * @param $condition
     * @return int
     */
    public function cateDel($condition){
        return $this->name('article_category')->where($condition)->delete();
    }

    /**
     * @param $condition
     * @param $data
     * @return $this
     */
    public function cateEdit($condition,$data){
        return $this->name('article_category')->where($condition)->update($data);
    }
}